<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        
    	// enable authentication 
    	$autentication = Zend_Registry::get('authetication');
    	if(!$autentication)
    	{ 
    		$this->_helper->redirector('login', 'index');
    	}

    	
    	$projects  = new Application_Model_DbTable_Projects()  ; 
    	//$select = $projects->select();
			
		$rows = $projects->fetchAll();
		$this->view->projects = $rows  ;
		$this->view->username = $autentication->username ;
		
		$commits = new Application_Model_DbTable_Commits() ;
		$select = $commits->select()
		->where('submited is null and author = \''.$autentication->username.'\' ');
		$rows = $commits->fetchAll($select);
		
		$this->view->commits = $rows  ;
		
		//print_r($rows) ; 
    	
    	//echo "show my projects" ;

    	
    }
    
    
    public function loginAction()
    {
        // action body
        
    	
    	//print_r($_SESSION) ; 
    	
    }
    
    public function logoffAction() 
    { 
    	
    	$authNamespace = new Zend_Session_Namespace('Zend_Auth');
    	
    	unset($authNamespace->user) ;
//    	unset($authNamespace->acl) ;
    	unset($authNamespace->role) ;
    	
    	Zend_Auth::getInstance()->clearIdentity();
    	$this->_helper->redirector('login', 'index');
    	
    	
    }
    
    
    private function loadprojectdata()
    {
    
    	$data = new Application_Model_DbTable_Projects() ;
    	$project = $data->find($_SESSION['Zend_Auth']['project']) ;
    	$row = $project->current();
    	return $row  ;
    
    }
    
    
    
    protected function _process($values)
    {
    	// Get our authentication adapter and check credentials
    	$adapter = $this->_getAuthAdapter();
    	$adapter->setIdentity($values['username']);
    	$adapter->setCredential($values['password']);
    
    	$auth = Zend_Auth::getInstance();
    	$result = $auth->authenticate($adapter);
    	if ($result->isValid()) {
    		
    		
    		$user = $adapter->getResultRowObject();
    		$auth->getStorage()->write($user);
    
    		
    		//$_SESSION["username"] = $values['username']; 
    		
    		$authNamespace = new Zend_Session_Namespace('Zend_Auth');
    		$authNamespace->user = $values['username'];
    		
    		
    		//$project_data = $this->loadprojectdata() ; 
    		
    		// get default project user 
    		/*$authNamespace->project = $project_data->id  ;
    		$authNamespace->project_name = $project_data->name ;
    		*/ 
    		
    		@$authNamespace->role = $user->role ; 
    			
    
    		return true;
    	}
    	return false;
    }
    
    
    protected function _getAuthAdapter() {
    
    	$dbAdapter = Zend_Db_Table::getDefaultAdapter();
    	$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
    
    	$authAdapter->setTableName('users')
    	->setIdentityColumn('username')
    	->setCredentialColumn('password');
    
    
    	return $authAdapter;
    }
    
    
    public function authenticateAction()
    {
    
    	$this->_helper->layout()->disableLayout();
    	$this->_helper->viewRenderer->setNoRender(true);
    	
    	
    	// if user already authenticated -- destroy
    	$authNamespace = new Zend_Session_Namespace('Zend_Auth');
    	unset($authNamespace->user) ;
    	Zend_Auth::getInstance()->clearIdentity();
    	 
    	// get a values 
    	$values['username'] = $_REQUEST['username'];
    	$values['password'] = $_REQUEST['password'];
    	 
  /*  	$teste = Zend_Registry::get('authetication');
    	 
    	print_r($teste) ;*/ 
    	
    	$auth = $this->_process($values) ;  
    	
    	if($auth) echo "1" ; 
    	
    	return $auth ;
    	
    	
    	
    } 
    
	public function changeprojectAction()
	{ 
		
		
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		
		$_SESSION['Zend_Auth']['project'] = $_REQUEST['id'] ;
		
		$project = $this->loadprojectdata() ; 
		
		$_SESSION['Zend_Auth']['project_name'] = $project->name  ;
		$_SESSION['Zend_Auth']['project_desc'] = $project->description  ;
		
		 
		
		
		
		
		
		$this->_helper->redirector('index','file');
		
		
		
	}     
    
    
    
    

}

